package com.gin.kotlin.gf2gamecommunity.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gin.kotlin.enums.QueryType
import com.gin.kotlin.form.ThemeForm
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedPagingData
import com.gin.kotlin.gf2gamecommunity.base.livedata.ParamCachedLiveData
import com.gin.kotlin.gf2gamecommunity.pagingsource.ThemePagingSource
import com.gin.kotlin.gf2gamecommunity.pagingsource.TopicInfoPagingSource
import com.gin.kotlin.gf2gamecommunity.routes.TopicListRoute
import com.gin.kotlin.response.Res
import com.gin.kotlin.response.theme.ThemeInfo
import com.gin.kotlin.response.topic.TopicInfo

class ThemeViewModel : ViewModel() {

    val themeDetail = ParamCachedLiveData<Res<ThemeInfo.Body>, ThemeInfo, Int>(
        requestMethod = { id -> App.INSTANCE.api.themeApi.getThemeDetail(ThemeForm(id)) },
        bodyTransform = { it.data?.themeInfo },
    )
    val topicList = CachedPagingData<TopicInfo, Int>(scope = viewModelScope) { TopicInfoPagingSource(TopicListRoute(queryType = QueryType.THEME, themeId = it)) }

    val themeList = CachedPagingData<ThemeInfo, Nothing?>(scope = viewModelScope) { ThemePagingSource() }

    companion object {
        private val TAG = ThemeViewModel::class.java.simpleName
    }
}